/**
 * Created by Dalyn on 2017/2/28.
 */
import React from "react";
import OrderTable from './OrderTable'
import './css/IndentManage.css'
import Confirm from '../../common/Confirm'
import InputBox from '../../common/InputBox'
import ReactKey from "../../util/ReactKey"
import OrderState_Item from "./OrderState_Item"
import OrderType_Item from "./OrderType_Item"
import ListOrderMutation from "./mutation/ListOrderMutation"
import Relay from "react-relay";

import {_Icon} from 'ANT_DE';
const Icon = _Icon;
/*导出组件*/
export default class IndentManage extends React.Component {
    constructor(props) {
        super(props);
        this.Style = {};
        this.state = {
            orders: [],
        };

        this.stateorder = ['全部:', '未完成:0,1,2,3', '已完成:4'];
        this.ordertype = ['全部:', '成衣订单:1', '辅料订单:2', '布料订单:3', '印染订单:4', '纱线订单:5'];
        this.status = '';
        this.type = '';
    }

    componentWillMount() {
        this.Style = {
            search: {
                width: 200,
                height: 20,
                borderRadius: '5px',
                borderColor:"#c2df88",
                paddingLeft: '5px'
            },
        }
    }

    componentDidMount() {
        this.refs['ods' + 0].chose();
        this.refs['odt' + 0].chose();
        this.requestdata(10, 1);
    }

    requestdata(pageSize, pageNum, oid) {
        Relay.Store.commitUpdate(
            new ListOrderMutation({
                status: this.status,
                orderType: this.type,
                pageNum: pageNum,
                pageSize: pageSize,
                token: '',
                orderId: oid || '',
                type:''
            }),
            {
                onFailure: () => {
                },
                onSuccess: (response) => {
                    /*debugger*/
                    let errorCode = response.listOrder.listOrder.errorCode;
                    if (errorCode == 0) {
                        let orders = response.listOrder.listOrder.orders;
                        let total = response.listOrder.listOrder.totalResult;
                        this.setState({
                            orders: orders,
                        });
                        this.refs.ordertable.setState({
                            total: total
                        })
                    }
                }
            }
        )
    }

    choseodstate(indexchose, value) {
        this.status = value;
        this.stateorder.map((context, index) => {
            if (indexchose != index) {
                this.refs['ods' + index].unchose();
            }
        });
        this.refs.oid.setState({
            text: ''
        });
        this.refs.ordertable.refs.pagination.setState({
            text: 1
        })
        this.requestdata(10, 1);
    }

    choseodtype(indexchose, value) {
        this.type = value;
        this.ordertype.map((context, index) => {
            if (indexchose != index) {
                this.refs['odt' + index].unchose();
            }
        });
        this.refs.oid.setState({
            text: ''
        });
        this.refs.ordertable.refs.pagination.setState({
            text: 1
        })
        this.requestdata(10, 1);
    }

    addorderstate() {
        return this.stateorder.map((context, index) => {
            return <OrderState_Item context={context} index={index} key={index + '-'}
                                    choseodstate={this.choseodstate.bind(this)} ref={'ods' + index}/>
        });
    }

    addordertype() {
        return this.ordertype.map((context, index) => {
            return <OrderType_Item context={context} index={index} key={index + '-'}
                                   choseodtype={this.choseodtype.bind(this)} ref={'odt' + index}/>
        });
    }

    searchod() {
        this.status = '';
        this.type = '';
        this.stateorder.map((context, index) => {
            this.refs['ods' + index].unchose();
        });
        this.ordertype.map((context, index) => {
            this.refs['odt' + index].unchose();
        });
        this.refs['ods' + 0].chose();
        this.refs['odt' + 0].chose();
        let oid = this.refs.oid.state.text
        this.requestdata(10, 1, String.trim(oid));
    }

    render() {
        return (
            <div className="Order_main">
                <div className="order_box _clear">
                    <div className="order_state">
                        <span className="order_state_title">订单状态：</span>
                        {
                            this.addorderstate()
                        }
                    </div>
                    <div className="order_state">
                        <span className="order_state_title">订单状态：</span>
                        {
                            this.addordertype()
                        }
                    </div>
                    <div className="accord_search">
                        <div className="search_order">
                            <InputBox style={this.Style.search} className="indent-border-none" defaultvalute="输入订单编号搜索" ref={'oid'}/>
                            <span className="search-btn_d9 hdent_manage-search-btn" onClick={this.searchod.bind(this)}>
                                <Icon type="search"></Icon>
                                搜索
                            </span>
                        </div>
                    </div>
                </div>
                <OrderTable ref={"ordertable"} orders={this.state.orders} requestdata={this.requestdata.bind(this)}/>
            </div>
        )
    }
}